/*
 * Please have a look at the following code! Its comments explain specifics
 * for CustJsonRPCDataSource. 
 * Also have a look at example_listgrid_fetch_answer.js to what answer is expected
 * 
*/

// CustJsonRPCDataSource is the class to use for JsonRPC.
isc.CustJsonRPCDataSource.create({
    ID: "countryListDS",
    // Currently there is a bug in SmartClient which means that if your
    // back end responses with http status codes != 200 in case of a server
    // error, SmartClient will run into problems when dataFormat = "json"
    // is used. As a work around you can use dataFormat = "custom".
    // But as soon as this bug is fixed you should use dataFormat = "json"!
    //
    // Notice: If your back end responds with http status code = 200 for
    // server errors you should already use dataFormat = "json".
    //
    // Also notice: Errors on application level (validation error, etc.)
    // should be sent back as successful JsonRPC call. See for the respective
    // examples!
    
    //dataFormat: "custom",
    dataFormat: "json",                     // use this as soon as bug is fixed.
    fields: [
        {name: "countryCode", title: "Country Code", primaryKey: true, canEdit: false},
        {name: "government", title: "Government"},
        {name: "capital", title: "Capital"},
        {name: "population", title: "Population"},
        {name: "independence", title: "Independence"}
    ],
    operationBindings : [
        // Normally you would define your JsonRPC calls the following way.
        // But for this example we point to a json file. Therefore its commented
        // out.
        //
        // Notice: dataURL points to your service. serverMethod specifies the
        // respective method of your service. dataProtocol should be
        // "postMessage" for a http POST or "getParams" for a http GET.
        // No other dataProtocols are supported currently.
        
        //{ operationType:"fetch", dataURL:"/rpc/countries/", serverMethod:"query_country", dataProtocol:"postMessage"},
        //{ operationType:"add", dataURL:"/rpc/countries/", serverMethod:"add_country", dataProtocol:"postMessage"},
        //{ operationType:"update", dataURL:"/rpc/countries/", serverMethod:"update_country", dataProtocol:"postMessage"},
        //{ operationType:"remove", dataURL:"/rpc/countries/", serverMethod:"remove_country", dataProtocol:"postMessage"}
        
        { operationType:"fetch", dataURL:isc.Page.getAppFilesDir() + "example_listgrid_fetch_answer.js", serverMethod:"query_country", dataProtocol:"getParams"}
    ]
});

/*
 * Only SmartClient standard code from here on.
 * 
*/

isc.ListGrid.create({
    ID: "countryList",
    autoDraw: false,
    width: 500, height: 224,
    
    alternateRecordStyles: true,
    emptyCellValue: "--",
    dataSource: countryListDS,
    fields:[
        {name: "countryCode"},
        {name: "government"},
        {name: "capital"},
        {name: "population"},
        {name: "independence"}
    ],
    sortFieldNum: 0, // sort by countryCode
    dataPageSize: 50,
    autoFetchData: true
});


isc.DynamicForm.create({
    ID: "dynamicFormCountries",
    autoDraw: false,
    //numCols: 4,
    //minColWidth: 110,
    fields: [
        {name: "validateBtn", ID: "buttonFetch", title: "fetch", type: "button", click: "countryList.data.invalidateCache(); countryList.fetchData();"}
    ]
});

isc.VLayout.create({
    ID: "vLayoutView",
    autoDraw: false,
    members: [ countryList, dynamicFormCountries ]
});

isc.TabSet.create({
    ID: "tabSetCountries",
    autoDraw: false,
    showEdges: true,
    backgroundColor: "#FFFFFF",
    tabBarAlign: "left",
    destroyPanes: "false",
    tabs: [
        {   ID: "TabView",
            title: "example_listgrid_fetch.js",
            pane: vLayoutView
        }
    ]
});

isc.VLayout.create({
    ID: "vLayoutExample",
    autoDraw: false,
    members: [ tabSetCountries ]
});


